# templates/controller.py.j2
from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from sqlalchemy.ext.asyncio import AsyncSession
from src.utils.auth.auth import AllUserAuth
from src.db.db_database import alchemy
from src.core.base_controller import BaseController
from src.core.dependencies import IdList
from src.core.response.response_schema import ResponseGenericSchema, ResponseSchema, PageResponseDataSchema
from src.schemas.{{ table_name }}_schema import (
    {{ model_name }}Params,
    {{ model_name }}Out,
    {{ model_name }}Update,
    {{ model_name }}Create,
)
from src.models.{{ table_name }}_model import {{ model_name }}
from src.services.{{ table_name }}_service import {{ model_name }}Service

router = APIRouter()


@cbv(router)
class PostController(BaseController):
    db: AsyncSession = Depends(alchemy.provide_session())
    {{ table_name }}_service: {{ model_name }}Service = Depends(alchemy.provide_service({{ model_name }}Service))

    @router.get(
        "/list",
        summary="{{ describe }}列表",
        tags=["{{ tags }}"],
        response_model=ResponseGenericSchema[PageResponseDataSchema[{{ model_name }}Out]],
        dependencies=[Depends(AllUserAuth())],
    )
    async def get_post_list(self, param: {{ model_name }}Params = Depends()):
        where = await param.get_where_and_limit_offset(self.db)
        res, count = await self.{{ table_name }}_service.list_and_count(*where)
        return self.paginated_response(data=res, total=count, current=param.page, size=param.limit)


    @router.post(
        "/add",
        summary="创建{{ describe }}",
        description="创建{{ describe }}",
        tags=["{{ tags }}"],
        dependencies=[Depends({{ 'PermissionAuth(["{{ permissions }}:add"])' if permissions else 'AllUserAuth()' }})],
        response_model=ResponseSchema,
    )
    async def add_{{ table_name }}(
        self,
        model: {{ model_name }}Create,
    ):
        await self.{{ table_name }}_service.create(model, auto_commit=True)
        return self.success(message="创建成功")

    @router.put(
        "/update",
        summary="修改{{ describe }}",
        description="修改{{ describe }}",
        tags=["{{ tags }}"],
        dependencies=[Depends({{ 'PermissionAuth(["{{ permissions }}:update"])' if permissions else 'AllUserAuth()' }})],
        response_model=ResponseSchema,
    )
    async def update_{{ table_name }}(
        self,
        model: {{ model_name }}Update,
    ):
        await self.{{ table_name }}_service.update(model, auto_commit=True)
        return self.success(message="更新成功")

    @router.delete(
        "/delete",
        summary="删除{{ describe }}",
        description="删除{{ describe }}",
        tags=["{{ tags }}"],
        dependencies=[Depends({{ 'PermissionAuth(["{{ permissions }}:delete"])' if permissions else 'AllUserAuth()' }})],
        response_model=ResponseSchema,
    )
    async def delete_{{ table_name }}(
        self,
        ids: IdList = Depends(),
    ):
        await self.{{ table_name }}_service.delete_many(ids.ids, auto_commit=True)
        return self.success(message="删除成功")
